package nancal.mp.db.mongo.entity.dev

import nbcp.base.annotation.Require
import nbcp.base.db.annotation.DbEntityGroup
import nbcp.base.db.annotation.DbEntityIndex
import nbcp.base.db.annotation.DbEntityIndexes
import nbcp.base.extend.HasValue
import nbcp.base.model.BaseMarkEntity
import org.springframework.data.mongodb.core.mapping.Document


data class DbSetting(
    var host: String = "",
    var port: Int = 0,
    var user: String = "root",
    var password: String = "",
//    var targetDockerImage:String = "",
    var dbs: String = ""
) {
    fun isValidate(): Boolean {
        return this.host.HasValue && this.dbs.HasValue && this.user.HasValue
    }


    fun getMongoUri(db: String): String {
        if (port <= 0) {
            return "";
        }
        return "mongodb://${user}:${password}@${host}:${port}/${db}";
    }

    fun getMysqlUri(db: String): String {
        if (port <= 0) {
            return "";
        }

        var query = "?";

        if (user.HasValue) {
            query += "user=${user}&"
        }

        if (password.HasValue) {
            query += "password=${password}&"
        }
        return "jdbc:mysql://${host}:${port}/${db}${query}useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai"
    }

    fun getMariadbUri(db: String): String {
        if (port <= 0) {
            return "";
        }

        var query = "?";

        if (user.HasValue) {
            query += "user=${user}&"
        }

        if (password.HasValue) {
            query += "password=${password}&"
        }
        return "jdbc:mariadb://${host}:${port}/${db}${query}useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai"
    }
}


@Document
@DbEntityGroup("dev")
@DbEntityIndexes(DbEntityIndex("createAt", unique = false))
open class JenkinsOpsLog(

    var namespace: String = "",

    var k8sServices: List<String> = listOf(),

    @Require
    var size: Int = 0,
    var mongo: DbSetting = DbSetting(port = 27017),
    var mysql: DbSetting = DbSetting(port = 3306),

    var targetEnv: String = "",

    var buildNumber: Int = 0

) : BaseMarkEntity() {
}

